package com.sharding.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sharding.entity.Account;
import com.sharding.mapper.AccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author sw
 * @create 2023-05-04 16:51
 */

@RestController
@RequestMapping("/account")
@ResponseBody
public class AccountController {

    @Autowired
    AccountRepository accountRepository;

    @GetMapping("/list")
    public String getPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        Page<Account> accountPage = accountRepository.selectPage(new Page<>(page, pageSize), queryWrapper);

        return JSON.toJSONString(accountPage);
    }

    @GetMapping("/total")
    public Long getTotal() {
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        return accountRepository.selectCount(queryWrapper);
    }
}
